package yxy.game.pm2.module.union;

import yxy.apple.util.PubSub.MethodSubscriber;
import yxy.apple.util.PubSub.SubscriptionMethod;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.pubsub.PlayerBeanPublisher;
import yxy.game.pm2.bean.pubsub.PlayerBeanPublisher.PlayerBeanSubscription;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;

public class UnionSubscriber_总战力 extends MethodSubscriber {

	static private UnionSubscriber_总战力 instance;

	public static UnionSubscriber_总战力 getInstance() {
		if (instance == null) {
			instance = new UnionSubscriber_总战力();
			instance.init();
		}
		return instance;
	}

	private void init() {
		PlayerBeanPublisher.getInstance().subscribe(this);
	}

	private UnionSubscriber_总战力() {
	}

	@SubscriptionMethod(Type = PlayerBeanSubscription.class)
	public void receive(PlayerBeanSubscription subscription) {
		PlayerBean playerBean = subscription.getPlayerBean();
		UnionPlayerEntry entry = new UnionPlayerEntry(playerBean.getCode());
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		try {
			if (uLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();
				new UnionHandle().powerUpdate(union);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			uLock.unlock();
		}
	}

}
